
package com.monlyu.bo;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.monlyu.bean.Page;
import com.monlyu.bean.Pages;
import com.monlyu.util.StringUtil;
import com.monlyu.util.WebOptUtil;
import com.monlyu.vo.DeviceSupply;

/**
 * @author SongJun
 * @since 2010-3-7
 * @description
 */
@Repository(value = "deviceSupplyBo")
public class DeviceSupplyBo extends BaseBo {

    public void updateSupply(DeviceSupply ds){
        DeviceSupply dbds = getDeviceSupplyById(ds.getId());
        // dbds.setEnable(ds.isEnable());
        dbds.setLinkAddr(ds.getLinkAddr());
        dbds.setLinkName(ds.getLinkName());
        dbds.setLinkPhone(ds.getLinkPhone());
        dbds.setSupplyDesc(ds.getSupplyDesc());
        dbds.setUnitName(ds.getUnitName());
        dbds.setUser(WebOptUtil.curUser());
        dao.update(dbds);
    }

    public DeviceSupply getDeviceSupplyById(Long id){
        String hql = "from com.monlyu.vo.DeviceSupply s where s.id =?";
        return (DeviceSupply) dao.findOne(hql, id);
    }

    public void insertDeviceSupply(DeviceSupply ds){
        ds.setUser(WebOptUtil.curUser());
        dao.insertOrUpdate(ds);
    }

    public void toEnable(Long id, boolean flag){
        DeviceSupply dbds = getDeviceSupplyById(id);
        dbds.setEnable(flag);
        dao.update(dbds);
    }

    public Pages<DeviceSupply> getDeviceSupply(Page page){
        Criteria criteria = dao.initCriteria(DeviceSupply.class);
        if (!StringUtil.isEmpty(page.getKey())) {
            criteria.add(Restrictions.like("unitName", page.getKey().trim(), MatchMode.ANYWHERE)).add(
                Restrictions.like("linkName", page.getKey().trim(), MatchMode.ANYWHERE));
        }
        criteria.addOrder(Order.asc("unitName"));
        return findByCriteria(page.getNo(), page.getSize(), criteria);
    }
}
